/*
 * prompt_tsk.c
 *
 *  Created on: 2011/11/02
 *      Author: AKS
 */
/*
 * prompt_tsk.c
 *
 *  Created on: 2011/11/02
 *      Author: AKS
 */
#include "core/include/aks_processor.h"
#include "core/include/process.h"
#include "core/include/types.h"
#include "core/include/aks_string.h"
#include "core/include/queue.h"

#include "include/prompt_tsk.h"
#include "app/cv/include/aksDraw.h"

/* ------ prompt input callback ------*/
FIFO_BUF lineBuf;
BOOL EOLFlag;

void prompt_uart_callback(BYTE c)
{
    Enqueue(&lineBuf, c);

    return;
}

/* ------ Process Core ------ */
PROCESS prompt_process =
{
	prompt_tsk,
    NULL,
    prompt_tsk_wakeup,
    NULL,
    0
};

LIST_ITEM promptItm
=
{
    NULL,
    &prompt_process
};

/* ------ display prompt buffer ------ */
#define PROMPT_COL  50
#define PROMPT_ROW  1
WORD colSize = PROMPT_COL;
WORD rowSize = PROMPT_ROW;

BYTE promptBuffer[PROMPT_ROW][PROMPT_COL];
WORD rowPtr;
WORD colPtr;

void prompt_tsk(void *param)
{
	BYTE c;

	while(Dequeue(&lineBuf, &c))
	{
		// convert to Ascii Format
		c = c & 0x7F;

		if(c == '\n')
		{
		    colPtr = 0;
		    rowPtr = (rowPtr + 1) % PROMPT_ROW;

		    for(BYTE i=0; i<PROMPT_COL ; ++i) promptBuffer[rowPtr][i] = '\0';
		}
		else if(colPtr >= PROMPT_COL)
		{
		    colPtr = 0;
		    rowPtr = (rowPtr + 1) % PROMPT_ROW;

		    for(BYTE i=0; i<PROMPT_COL ; ++i) promptBuffer[rowPtr][i] = '\0';

			promptBuffer[rowPtr][colPtr++] = c;
		}
		else
		{
			promptBuffer[rowPtr][colPtr++] = c;
		}
	}

	return;
}

BOOL prompt_tsk_wakeup(void *param)
{
	// wake if any input from User
	return (QUEUE_FILLED_LENGTH(lineBuf.r_ptr, lineBuf.w_ptr) > 0) ? 1 : 0;
}

BOOL init_prompt_tsk(BYTE SS_index)
{
	UART_setReceiveCallback(UART_DBG, prompt_uart_callback);

	rowPtr = 0;
	colPtr = 0;

	Init_FIFO_Buffer(&lineBuf);

	return TRUE;
}


/* drawing prompt */
BOOL draw_prompot(WORD x, WORD y, AksImage *tgtImg)
{
	akdDrawFilledRect(tgtImg, aksPoint(x, y), aksPoint(x+300, y+(12*rowSize)), aksPixel(0,0,0));
	aksDrawStrings(tgtImg, aksPoint(x+1,y+1), aksPixel(51,153,255), promptBuffer[0] );

	aksLine(tgtImg, aksPoint(x,y + 12*rowSize), aksPoint(x + 300, y + 12*rowSize), aksPixel(51,153,255) );

	return TRUE;
}
